<?php
/* @var $this RoleController */

$this->breadcrumbs=array(
	'Phân quyền',
);
?>
<div class="wap-search" style="width: 40%;">      
      <div class="form-search" id="<?php echo Yii::app()->controller->id . '-' . Yii::app()->controller->action->id?>">
   
         <?php $form=$this->beginWidget('CActiveForm', array(
         	'id'=>Yii::app()->controller->id . '_' . Yii::app()->controller->action->id,
         	'enableAjaxValidation'=>false,
            'enableClientValidation'=>true
         )); ?>
         	<?php echo $form->errorSummary($model,'Kiểm tra lại các giá trị bạn vừa nhập vào'); ?>
            <input name="session_user" type="hidden" value="<?php echo $session_user?>"/>
            <input name="action_user" type="hidden" value="search" id="action_user" />
               <table>
                  <tr>
                     <td>
                     <?php echo $form->dropDownList($model, 'MemberGroupID',$groupList);?>
                     </td>
                     <td>
                     <?php echo $form->dropDownList($model, 'MenuAdminID',$menu_tree);?>
                     </td>
                     <td><input class="btn" type="submit" value="Tìm kiếm" /></td>
                  </tr>
               </table>
         <?php $this->endWidget(); ?>
         </div> 
</div>
<div class="list-role">
   <div class="list-menu" id="grid">
      <table>
         <tr class="GridHeader">
            <th class=""><input class="row-check-all" type="checkbox" id="chk_SelectAllRow"></th>
            <th style="width:36px;"><div>STT</div></th>
            <th><div><span class="GridColumnSort">Tên nhóm</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
            <th><div><span class="GridColumnSort">Menu</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
            <th><div><span class="GridColumnSort">Xem</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
            <th><div><span class="GridColumnSort">Thêm</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
            <th><div><span class="GridColumnSort">Sửa</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
            <th><div ><span class="GridColumnSort">Xóa</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
            <th><div ><span class="GridColumnSort">Kích hoạt</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
            <th><div><span class="GridColumnSort">Duyệt</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
            <th><div><span class="GridColumnSort">Import</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
            <th><div><span class="GridColumnSort">Xuất</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
        </tr>
        <?if(count($list) > 0){
         $i = 0;
         foreach($list as $item){
            $i++;
            ?>
            <tr class="grid-slow-<?php echo $i%2?>">
               <td class="select-row">
                  <input class="row-check" type="checkbox" value="<?php echo $item['RoleID']?>"/>
               </td>
               <td align="center"><?php echo $i?></td>
               <td><?php echo $item['MembergroupName']?></td>
               <td><?php echo $item['MenuName']?></td>
               <?php echo ($item['IsView'] == 1) ? CHtml::tag('td',array('class' => 'role accept'),'<span>Yes</span>') : CHtml::tag('td',array('class' => 'role denied'),'<span>No</span>') ?>
               <?php echo ($item['IsAdd'] == 1) ? CHtml::tag('td',array('class' => 'role accept'),'<span>Yes</span>') : CHtml::tag('td',array('class' => 'role denied'),'<span>No</span>') ?>
               <?php echo ($item['IsEdit'] == 1) ? CHtml::tag('td',array('class' => 'role accept'),'<span>Yes</span>') : CHtml::tag('td',array('class' => 'role denied'),'<span>No</span>') ?>
               <?php echo ($item['IsDelete'] == 1) ? CHtml::tag('td',array('class' => 'role accept'),'<span>Yes</span>') : CHtml::tag('td',array('class' => 'role denied'),'<span>No</span>') ?>
               <?php echo ($item['IsActive'] == 1) ? CHtml::tag('td',array('class' => 'role accept'),'<span>Yes</span>') : CHtml::tag('td',array('class' => 'role denied'),'<span>No</span>') ?>
               <?php echo ($item['IsPublish'] == 1) ? CHtml::tag('td',array('class' => 'role accept'),'<span>Yes</span>') : CHtml::tag('td',array('class' => 'role denied'),'<span>No</span>') ?>
               <?php echo ($item['IsImport'] == 1) ? CHtml::tag('td',array('class' => 'role accept'),'<span>Yes</span>') : CHtml::tag('td',array('class' => 'role denied'),'<span>No</span>') ?>
               <?php echo ($item['IsExport'] == 1) ? CHtml::tag('td',array('class' => 'role accept'),'<span>Yes</span>') : CHtml::tag('td',array('class' => 'role denied'),'<span>No</span>') ?>
          </tr>
         <?}
        }?>
       
      </table>
      <form class="form" id="option_form" action="" method="POST">
         <table>
            <tr>
               <td>  
                  Số dòng:
                  <select name="limit" id="limit">
                     <?foreach($rowList as $value){?>
                        <option value="<?php echo $value?>" <?php if($value == $limit) echo 'selected'?> > <?php echo $value?></option>
                     <?}?>
                  </select>
               </td>
               <td class="text-center">
                  Trang:
                  <input id="page_input" name="page" type="text" value="<?php echo $page?>" />
                  Tổng số:
                  <?php echo $total_pages?>
               </td>
               <td class="text-right">
                  Xem:
                     <?php echo $start + 1?>
                      - 
                     <?php 
                     $end = $limit*$page + 1;
                     $end = ($end > $total_record) ? $total_record : $end;
                     echo $end ?>
                  Tổng số:
                     <?php echo $total_record?>
               </td>
            </tr>
         </table>
      </form>
   </div>
</div>
<script>
   $(document).ready(function(){
      //Select all record
      $("#chk_SelectAllRow").click(function(){
         var checkboxes = $(".row-check");
         if($(this).is(':checked')) {
            checkboxes.attr('checked', 'checked');
         } else {
            checkboxes.removeAttr('checked');
         }
      });
      
      //Submit form when limit change
      $("#limit").change(function(){
         $("#option_form").submit();
      });
      
      //Submit form when page change
      var old_page;
      var new_page;
      $("#page_input").focus(function(){
         old_page = $(this).val();
      });
      $("#page_input").blur(function(){
         new_page = $(this).val();
         if(new_page !== old_page) $("#option_form").submit();
      });
      $("#active").remove();
      $("#deactive").remove();
      
      //Delete record
      $("#delete").click(function(){
         var list_id = [];
         $("input.row-check").each(function(){
            if($(this).is(":checked")){
               list_id.push($(this).val());
            }
         });
         if(list_id.length === 0){
            alert("Bạn phải chọn ít nhât 1 bản ghi");
         }else{
            var data = {'list_id':list_id,'session_user':'<?php echo $session_user?>'};
            var url = '<?php echo CController::createUrl('delete')?>';
            var succ = function(data){
               
               if(data.error == 0){
                  alert("Có " + data.record_affected + " bản ghi bị xóa");
                  location.reload();
               }
            }
            var _confirm = confirm("Bạn có chắc chắn thực hiện thao tác này ?");
            if(_confirm)
               handleAjax(url,"POST","json",data,succ);
         }
         
      });
      
      //Edit record 
      var id_edit = 0;
      $(".row-check").click(function(){
         id_edit = $(this).val();
      });
      $("#edit").click(function(){
         if(id_edit == 0){
            alert("Bạn phải chọn 1 bản ghi để sửa")
         }else{
            window.location.href = '<?php echo CController::createUrl('edit')?>' + '/' + id_edit;
         }
      });
         
   });
</script>
